// pages/PersonalCenter/BaskInTheSun/BaskInTheSun.js
const Controller = require('../../controllers/controllers').controller
const util = require('../../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '晒一晒', //晒一晒，小程序名片
    showPhoneIcon: false,
    template: {}, //海报图片生成配置
    userInfo: {},
    telephone: '', //手机号码
    saveImage: '',
    cavImg: 'https://tse1-mm.cn.bing.net/th/id/OIP.bNNaDh0yqigvzZYSCSne_AHaKP?pid=Api&rs=1',
    logo: 'https://youyang-cdn.mdshuzhi.com/upload/pic_logo@2x.png',
    QRCode: 'https://youyang-cdn.mdshuzhi.com/upload/youyang_or.png', //二维码图片
    width: 600,
    height: 900,
    shareWord: '',
    talent: "",
    options: {},
    hasLenLeft: 240,
    istipsnext:true,
    isfirsttips:false,
    topbg:'',
    bottombg:'',
    tipstop:'',
    tipsbottom:'',
    bgImg1:'',
  },
  // 展示手机icon
  showPhoneIcon(e) {
    console.log('展示手机icon', e)
    let telephone = wx.getStorageSync('userInfos').mobile || ''
    let showPhoneIcon = !this.data.showPhoneIcon
    let that = this
    if (this.data.userInfo.nick.length > 6) {
      this.setData({
        hasLenLeft: 270
      })
    } else if (this.data.userInfo.nick.length <= 4) {
      this.setData({
        hasLenLeft: 210
      })
    }
    // showPhoneIcon false为选择显示手机号
    this.setData({
      showPhoneIcon,
      telephone: !this.data.showPhoneIcon ? ' | ' + telephone : ''
    })
    if (this.timer) {
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(() => {
      that.getDraw()
    }, 300)
  },
  // 保存图片
  saveImgBtn() {
    console.log('保存图片')
    wx.showLoading({
      title: '保存中',
      mask: true,
    })
    app.saveImg(this.data.saveImage)

  },
  // 绘制成功
  onImgOK(res) {
    /// 返回生成 base64 图片的本地 url,设置 src
    console.log('绘制成功', res)
    wx.hideLoading()
    this.setData({
      saveImage: res.detail.path
    })
  },
  // 绘制失败
  onImgErr(err) {
    console.log('绘制失败', err)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    let userInfo = wx.getStorageSync('userInfos')
    console.log('userInfo', userInfo)
    let type = '' //达人榜4 晒一晒9
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      if (options && options.type == 1) { //是否为达人
        options.listenTimes = util.toHour(options.allTime)
        type = 8 //8达人主页 晒一晒9
        this.setData({
          title: '达人分享',
          shareWord: '为你分享达人',
          options,
          talent: '达人“' + options.nick + '”'
        })
      } else {
        // 1合辑 2栏目 3音频 4达人榜 5推荐听单，6历史听单，7我的收藏，8达人主页，9晒一晒，10小程序名片
        type = 9 //8达人主页 晒一晒9
        let options = {}
        options.monthRank = userInfo.monthRank
        options.weekRank = userInfo.weekRank
        options.listenTimes = util.toHour(userInfo.listenTimes)
        this.setData({
          title: '晒一晒',
          shareWord: '来有氧，一起听吧',
          options
        })
      }
      Controller.shareImg({
          type,
          token: wx.getStorageSync('logintoken')
        })
        .then(res => {
          console.log(res)
          if (res.code == 0) {
            this.setData({
              cavImg: res.data.baseData,
              userInfo
            })
            this.makeShare();
          }
        })
    }
    if(app.globalData.firsttips2){
      this.firsttips()
    }

  },
  makeShare(options,type) {
    let path="pages/Index/Index"
    var options=this.data.options;
    let data={source: 1,path}
    data.scene=1
    data.type=type || 2
    data.itemImg=this.data.cavImg
    Controller.makeShare(data).then(res => {
        if (res.code == 0) {
          let bgImg = res.data.baseData
          this.setData({
            bgImg1:bgImg
          })
          this.getDraw()
        }
      })
  },
  getPageHeight(){
      let systemInfo = wx.getSystemInfoSync()
      let windowHeight = systemInfo.screenHeight
      console.log(systemInfo)
      if(systemInfo.model.indexOf("iPhone X")!=-1){
        windowHeight=windowHeight;
        this.setData({
          isIphoneX:true
          
        })
      }
      this.setData({
        windowHeight,
        barheight:systemInfo.statusBarHeight
      })
      this.tips()
  },
  tips(){
    var that=this;
    let query = wx.createSelectorQuery()
    query.select('#chooseBox_showPhone').boundingClientRect((rect) => {
      if(rect){
        let top = rect.top;
        let bottom = rect.bottom;
        let height = rect.height;
        if(that.data.isIphoneX){
          top=top+33
        }
        console.log(top+'||'+bottom+'||'+height+'||'+that.data.barheight)
        that.setData({
          tipstop: top + height + that.data.barheight/2,
          tipsbottom:that.data.windowHeight - top + that.data.barheight/2
        })
      }
    }).exec()
  },
  // 绘画海报
  getDraw() {
    wx.showLoading({
      title: '正在努力生成中',
    })
    this.setData({
      template: {
        background:this.data.bgImg1,
        width: this.data.width + 'rpx',
        height: this.data.height + 'rpx',
        views: [{
          type: 'text',
          text: this.data.talent, //图片上的用户名
          css: {
            height: '40rpx',
            top: '450rpx',
            left: '30rpx',
            fontSize: '40rpx',
            color: "#fff",
          },
        }, {
          type: 'text',
          text: '周排名' + (this.data.options.weekRank || 0), //周排名
          css: {
            width: '200rpx',
            top: '534rpx',
            left: '0rpx',
            fontSize: '22rpx',
            color: "#fff",
            lineHeight: "66rpx",
            textAlign: "center"
          },
        }, {
          type: 'text',
          text: '月排名' + (this.data.options.monthRank || 0), //月排名
          css: {
            width: '200rpx',
            top: '534rpx',
            left: '200rpx',
            fontSize: '22rpx',
            color: "#fff",
            lineHeight: "66rpx",
            textAlign: "center"
          },
        }, {
          type: 'text',
          text: '总时长' + (this.data.options.listenTimes) , //总时间
          css: {
            width: '200rpx',
            top: '534rpx',
            left: '400rpx',
            fontSize: '22rpx',
            color: "#fff",
            lineHeight: "66rpx",
            textAlign: "center"
          },
        }, {
          type: 'text',
          text: this.data.userInfo.nick + this.data.telephone, //用户名
          css: {
            maxLines: '1',
            height: '40rpx',
            bottom: '240rpx',
            left: '100rpx',
            fontSize: '20rpx',
            color: "#A8ACBB"
          },
        },  {
          type: 'text',
          text: this.data.shareWord, //为你分享达人
          css: {
            lineHeight: "25rpx",
            bottom: '200rpx',
            left: '100rpx',
            fontSize: '24rpx',
            color: "#323232",
          },
        }, {
          type: 'text',
          text: this.data.options.nick, //达人
          css: {
            width:'480rpx',
            maxLines:'1',
            lineHeight: "25rpx",
            bottom: '240rpx',
            left: '90rpx',
            fontSize: '20rpx',
            color: "#A8ACBB",
          },
        }, {
          type: 'text',
          text: '有氧FM·音频分享',
          css: {
            left: "200rpx",
            bottom: '55rpx',
            fontSize: '20rpx',
            color: "#A8ACBB",
          },
        }],
      },
    })
  },
  // 生成二维码
  /* 
    参数：
    path：二维码扫描后跳转的页面
    scene：参数  如 a=3
  */
  // makeShareImg() {
  //   Controller.makeShareImg({
  //       source: 1,
  //       path: 'pages/Index/Index',
  //       scene:1
  //     })
  //     .then(res => {
  //       console.log('生成二维码', res)
  //       if (res.code == 0) {
  //         this.setData({
  //           QRCode: res.data.baseData
  //         })
  //         this.getDraw()
  //       }
  //     })
  // },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log('分享按钮', e)
    // if (e.from == "button") {
      let obj = {
        title: this.data.title,
        imageUrl: this.data.saveImage,
        path:'/pages/Index/Index',
        success: function (res) {
          console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
        },
        fail(err) {
          console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
        }
      }
      app.updateScore(12)
      return obj
    // }
  },
  onShareTimeline: function (res) {
    console.log('分享按钮', res)
    // if (res.from == "button") {
      let obj = {
        title: this.data.title,
        imageUrl: this.data.saveImage,
        path:'/pages/Index/Index',
        success: function (res) {
          console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
        },
        fail(err) {
          console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
        }
      }
      app.updateScore(12)
      return obj
    // }
  },
  firsttips(){
    let that=this;
    that.setData({
      isfirsttips:true
    })
    app.globalData.firsttips2=false
    that.getPageHeight()
  },
  tipsclose(){
    this.setData({
      isfirsttips:false
    })
  },
})